package cn.avalon.org.controller;

import cn.avalon.basic.exception.BusinessException;
import cn.avalon.basic.utils.AjaxResult;
import cn.avalon.basic.utils.ExcelUtils;
import cn.avalon.basic.utils.PageList;
import cn.avalon.org.domain.Shop;
import cn.avalon.org.domain.ShopAuditLog;
import cn.avalon.org.dto.ShopDto;
import cn.avalon.org.query.ShopQuery;
import cn.avalon.org.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description: 店铺接口类
 * @author: Avalon
 * @date: 2022年08月21日 ： 20:18
 * @version: ver1.0
 * @email 709785494@qq.com
 */
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public ShopService shopService;

    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：删除
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value="/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：批量删除
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids){
        try {
            shopService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
     * 接口：查询单个对象
     * @param id
     */
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id")Long id) {
        return shopService.findById(id);
    }

    /**
     * 接口：查询所有
     * @return
     */
    @GetMapping
    public List<Shop> findAll(){
        return shopService.findAll();
    }

    /**
     * 接口：分页查询或高级查询
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }

    @PostMapping(value="/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (BusinessException e) { // 捕捉自己抛出的异常
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) { // 500
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog) {
        System.out.println(shopAuditLog);
        try {
            shopService.auditPass(shopAuditLog);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统异常，请稍后重试");
        }
    }

    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.auditReject(shopAuditLog);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统异常，请稍后重试");
        }
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        try {
            List<Shop> shops = shopService.findAll();
            ExcelUtils.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file")MultipartFile file) {
        //String file = "F:\\文件名.xls";
        //解析excel，
        List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);
        shops.forEach(System.out::println);
    }

    @GetMapping("/echarts")
    public List<ShopDto> echarts() {
        List<ShopDto> shopDtos = shopService.echarts();
        return shopDtos;
    }
}
